package com.example.controller.buyer;

import com.example.constant.NumConstant;
import com.example.service.UserMessageService;
import com.example.vo.LoginUser;
import com.example.vo.MemberMessageQueryVO;
import com.example.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @author circle48
 */
@RestController
@Api(tags = "买家端,会员站内消息接口")
@RequestMapping("/buyer/message")
public class MessageBuyerController {
    @Resource
    private UserMessageService userMessageService;

    @ApiOperation(value = "分页获取会员站内消息")
    @GetMapping
    public Result page(MemberMessageQueryVO memberMessageQueryVO) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long id = loginUser.getUser().getId();
//        long id=1;
        memberMessageQueryVO.setUserId(id);
        return userMessageService.getMessages(memberMessageQueryVO);
    }

    @ApiOperation(value = "消息已读")
    @ApiImplicitParam(name = "messageId", value = "会员消息id", required = true, paramType = "path")
    @PutMapping("/{message_id}")
    public Result<Boolean> read(@PathVariable("message_id") Integer messageId) {
        return Result.ok(userMessageService.editStatus(NumConstant.MESSAGE_ALREADY_READY, messageId),"消息已读");
    }

    @ApiOperation(value = "消息放入回收站")
    @ApiImplicitParam(name = "messageId", value = "会员消息id", required = true, paramType = "path")
    @DeleteMapping("/{message_id}")
    public Result<Boolean> deleteMessage(@PathVariable("message_id") Integer messageId) {
        return Result.ok(userMessageService.editStatus(NumConstant.MESSAGE_ALREADY_REMOVE, messageId),"消息已放入回收站");

    }
}
